// refable.h

// Copyright (C) 2010  traxex

// Author: 	traxex <traxex@yahoo.cn>
// Created: 	14 Aug 2010
// Version: 	1.0

#pragma once

namespace traxex{

template<typename super>class refable{
  protected:
    refable(){
        count = new int(1);
        debugmsg( "create ref object %p", count);
    }

    refable(const refable& src): count(src.count){
        ++*count;
    }

    refable& operator=( const refable& src){
        ++*src.count;
        --*count;
        if( *count==0){
            debugmsg("unref object %p", count);
            delete count;
            count = 0;
            ((super*)this)->super::_finalize();
        }
        count = src.count;
        return *this;
    }

    virtual ~refable(){
        --*count;
        if( *count==0 ){
            debugmsg("unref object %p", count);
            delete count;
            count = 0;
            ((super*)this)->super::_finalize();
        }

    }
  private:
    mutable int	*count;
};

}
